<?php

namespace Modules\SmallApi\Http\Controllers;

use HiCommon\Service\LogService;
use HiCommon\Service\OrderCheckpointService;
use HiCommon\Service\OrderPayService;
use HiCommon\Service\PayService;
use HiCommon\Service\PayYanSongDaService;
use Illuminate\Support\Facades\Request;

class PayWechatController extends BaseController
{

    /**
     * 年检预约单微信小程序支付-小程序
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function order_checkpoint_mini_pay()
    {
        try {
            $n_id = intval(Request::input('id',0));//年检预约单id
            $n_uid = intval(small_api_user_id());//用户id
            $s_pay_type = 'wxmini';
            $s_order_type = 'njyy';

            //开启事务
            shiwu_start();
            $result = OrderPayService::api_online_pay($s_pay_type,$s_order_type,$n_id,$n_uid);
            shiwu_end($result);

            return  $result;
        }
        catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }

//    /**
//     * 小程序-年检预约单-取消预约【已支付订单】
//     *
//     * @return array
//     * @author wumengmeng <wu_mengmeng@foxmail.com>
//     */
//    public function order_checkpoint_cancle()
//    {
//        try {
//            $n_id = intval(Request::input('id',0));//年检预约单id
//            $n_uid = intval(small_api_user_id());//用户id
//
//            //开启事务
//            shiwu_start();
//            $result = OrderCheckpointService::api_cancle_appointment($n_uid,$n_id);
//            shiwu_end($result);
//
//            return  $result;
//        }
//        catch (\Exception $exception) {
//            return hello_error('失败',[],$exception->getMessage());
//        }
//    }

    /**
     * 小程序-年检预约单-申请退款
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function order_checkpoint_apply_refund()
    {
        try {
            $n_uid = intval(small_api_user_id());//用户id
            $arr_input = Request::input();

            //开启事务
            shiwu_start();
            $result = OrderCheckpointService::api_apply_refund_for_more_vehicle($n_uid,$arr_input);
            shiwu_end($result);

            return  $result;
        }
        catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }

    /**
     * 小程序-年检预约单-取消订单【未支付订单】
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function order_checkpoint_cancle_notpay()
    {
        try {
            $n_id = intval(Request::input('id',0));//年检预约单id
            $n_uid = intval(small_api_user_id());//用户id

            //开启事务
            shiwu_start();
            $result = OrderCheckpointService::api_cancle_appointment_notpay($n_uid,$n_id);
            shiwu_end($result);

            return  $result;
        }
        catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }

    /**
     * 微信支付回调确认
     *
     * @return array|\Symfony\Component\HttpFoundation\Response
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function mini_app_pay_notify()
    {
        try {
            $pay_wechat = PayYanSongDaService::pay_wechat();
            $data = $pay_wechat->verify(); // 是的，验签就这么简单！
            $json_data = $data->all();

            //开启事务
            shiwu_start();
            $result = OrderPayService::api_notify_confirm_for_wechat_pay($json_data);
            shiwu_end($result);

            //通知支付宝服务器 确认支付成功
            if($result['state'] == 0){
                return $pay_wechat->success();
                //                return hello_success('确认成功');
            }
            else{
                return  $result;
            }
        }
        catch (\Exception $exception) {
//            $json_data->error_msg = $exception->getMessage();
            LogService::wright_log_for_wechat_error('Wechat error', $exception->getMessage());

            return hello_error('失败',[],$exception->getMessage());
        }
    }

//    public function pay_mini_refund(){
//        try {
//            $s_order_sn = trim(Request::input('sn',''));
//
//            //开启事务
//            shiwu_start();
//            $s_refund_type = 'wechat_mini_pay';
//            $s_order_sn = 'NJYY19120906042509347';
//            $s_out_trade_no = 'NJYY19120906042509347_988';
//            $n_total_fee = 2;
//            $n_refund_fee = 2;
//
//            $res = PayService::refund($s_refund_type,$s_order_sn,$s_out_trade_no, $n_total_fee, $n_refund_fee, $arr_option = []);
//            shiwu_end($res);
//            return  $res;
//        }
//        catch (\Exception $exception) {
//            return hello_error('失败',[],$exception->getMessage());
//        }
//    }
}